package cn.toleo.controller;


import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.toleo.utils.FastDFSClient;
import cn.toleo.utils.JsonUtils;
/**
 * 图片上传controller
 * @author Administrator
 *
 */
@Controller
public class PictureController {
	
	@Value("${IMAGE_SERVER_URL}")
	private String IMAGE_SERVER_URL;

	@RequestMapping("/pic/upload")
	
	@ResponseBody
	public String picUpload(MultipartFile uploadFile){
		try {
		//接受上传文件
		//获取拓展名
		String originalFilename = uploadFile.getOriginalFilename();
		String extName = originalFilename.substring(originalFilename.lastIndexOf(".")+1);
		
		//上传到图片服务器
		FastDFSClient fastDFSClient = new FastDFSClient("resource/client.conf");
		String url = fastDFSClient.uploadFile(uploadFile.getBytes(), extName);
		url=IMAGE_SERVER_URL+url;
		
		//响应图片url
		Map result = new HashMap<>();
		result.put("error", 0);
		result.put("url", url);
		return JsonUtils.objectToJson(result);
		
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Map result = new HashMap<>();
			result.put("error", 1);
			result.put("message", "失败");
			return JsonUtils.objectToJson(result);
		} 
	}
}
